---@class BattleSkillPanel:UIComponentBase
local BattleSkillPanel = Class("BattleSkillPanel",UIComponentBase)
local SKILL_NUM = 12
function BattleSkillPanel:OnCreate()
    ---@type GList
    --self.skillItemList = self.fguiObj
    self.itemList = {}
    for i = 0,SKILL_NUM-1 do
        local go
        if i < 10 then
            go = self:GetChild("Btn_Skill0"..i)
        else
            go = self:GetChild("Btn_Skill"..i)
        end
        go.visible = false
        local anim = go:GetTransition("scale")
        table.insert(self.itemList,{go = go,anim = anim,skill = nil})
    end

    ---@type Battle.Skill[]
    self.skillList = {}
    --self:SetItemRenderer(self.skillItemList,self.ItemRender,nil,true)
    self:Register()
    self:InitSkill()
end

function BattleSkillPanel:OnDestroy()
    self:UnRegister()
end
function BattleSkillPanel:Register()
    Battle.GlobalEventSystem.Register(Battle.Event.SkillCDChange, self.Event_SkillCDChanged,self)
    Battle.GlobalEventSystem.Register(Battle.Event.ActorDead, self.Event_ActorDead,self)

end
function BattleSkillPanel:UnRegister()
    Battle.GlobalEventSystem.UnRegister(Battle.Event.SkillCDChange, self.Event_SkillCDChanged,self)
    Battle.GlobalEventSystem.UnRegister(Battle.Event.ActorDead, self.Event_ActorDead,self)
end

function BattleSkillPanel:InitSkill()
    local list = Battle.ActorManager.GetActorList()
    for k,v in pairs(list) do
        self:SkillCDChanged(v.skillSystem:GetSkill1())
    end
    self:SkillRefresh()
end

---@param obj GComponent
function BattleSkillPanel:ItemRender(idx)
    local skill = self.skillList[idx];

    local item = self.itemList[idx]
    if item.skill == skill then return end
    if skill == nil then
        item.go.visible = false
        item.skill = nil
        return
    end
    local actor = skill.actor
    local actorId = actor.id
    item.go.visible = true
    item.skill = skill
    local go = item.go
    go.title = "技能" .. skill.skillId
    go.icon = IconUtil.GetHeroSmallIconUrl(actor.modelId)
    item.anim:Play()
    go.onClick:Clear()
    go.onTouchMove:Clear()
    go.onTouchEnd:Clear()
    if skill:IsNeedHand() then
        go.onTouchMove:Add(function() self:OnTouchMove(actorId,skill)  end)
        go.onTouchEnd:Add(function()  self:OnTouchEnd(actorId,skill) end)
    else
        go.onClick:Add(function ()
            self:OnClickSkill(actor,skill)
        end)
    end
end
function BattleSkillPanel:SkillRefresh()
    --local count = #self.skillList
    --self.skillItemList.numItems = count
    for i = 1,SKILL_NUM do
        self:ItemRender(i)
    end
end
---@param actor Battle.Entity
function BattleSkillPanel:OnClickSkill(actor)
    actor.aiSystem:SetBlackboardValue("hand_skill",true)
end
function BattleSkillPanel:OnTouchMove(entityId,skill)
    local actor = Battle.ActorManager.GetActor(entityId)
    if not actor then return end
    actor.eventSystem:FireEvent(Battle.Event.SkillReleaseUpdatePos,Battle.EventArgs.SkillArgs:Create(skill))
end
function BattleSkillPanel:OnTouchEnd(entityId,skill)
    local actor = Battle.ActorManager.GetActor(entityId)
    if not actor then return end
    actor.eventSystem:FireEvent(Battle.Event.SkillEmit,Battle.EventArgs.SkillArgs:Create(skill))
end
---@param args BattleEvent
function BattleSkillPanel:Event_ActorDead(args)
    local actor = args.dead
    if actor.camp ~= Battle.CampType.Blue then
        return
    end
    local flag = false
    for k,v in pairs(self.skillList) do
        if v == actor.skillSystem:GetSkill1() then
            table.remove(self.skillList,k)
            flag = true
            break
        end
    end
    if flag then
        self:SkillRefresh()
    end
end
---@param args BattleEvent
function BattleSkillPanel:Event_SkillCDChanged(args)
    if args.skill.skillData.skillType ~= Battle.SkillType.Active then return end
    if args.sender.actorType == Battle.ActorType.Lord then return end
    if args.sender.camp ~= Battle.CampType.Blue then return end
    self:SkillCDChanged(args.skill)
    self:SkillRefresh()
    if not args.skill:IsInCD() then

        local item = self.itemList[#self.skillList]
        ---@type GObject
        local go = item.go
        local x,y = Util.FGUILocalToScreen(go,0,0,nil,nil)
        local global = go:LocalToGlobal(Vector2(50,50))
        global = GRoot.inst:GlobalToLocal(global);
        local pos = args.sender.pos
        local startX,startY = Util.WorldToFGUILocal(Battle.SceneMgr.camera,pos.x,pos.y,pos.z,nil,nil)
        UIBattle:ShowSkillReadyEff(startX,startY,global.x,global.y)
    end
end

---@param skill Battle.Skill
function BattleSkillPanel:SkillCDChanged(skill)
    if skill == nil then return end
    local actor = skill.actor
    if actor.camp ~= Battle.CampType.Blue then return end
    if actor.actorType == Battle.ActorType.Lord then return end
    if actor:IsDead() then return end
    if skill:IsInCD()  then
        for k,v in pairs(self.skillList) do
            if v == skill then
                table.remove(self.skillList,k)
                break
            end
        end
    else
        table.insert(self.skillList,skill)
    end
end

return BattleSkillPanel